namespace FinalProject.Data.Migrations
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

    using FinalProject.Entities;

    internal sealed class Configuration : DbMigrationsConfiguration<FinalProject.Data.FinalProjectContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(FinalProject.Data.FinalProjectContext context)
        {
            var address = new Address { City = "City1", Country = "US", State = "FL", Street1 = "Street1", Street2 = "Street2", ZipCode = 33322 };
            var companies = new List<Company>
            {
                new Company{ Name = "Company1",  PhoneNumber1=1234, Address =address },
                new Company{ Name = "Company2", PhoneNumber1=1234, Address = address}
            };
            companies.ForEach(c => context.Companies.Add(c));
            context.SaveChanges();

            var users = new List<User>
            {
                new User { UserName = "Admin", Email="admin@temp.com", CreationDate = DateTime.Now, LastActivityDate = DateTime.Now, LastLoginDate = DateTime.Now},
                new User { UserName = "Member", Email="member@temp.com", CreationDate = DateTime.Now, LastActivityDate = DateTime.Now,LastLoginDate = DateTime.Now}
            };
            users.ForEach(u => u.Password = HashPassword("password"));
            users.ForEach(u => context.Users.Add(u));
            context.SaveChanges();

            var userDetails = new List<UserDetails>
            {
                new UserDetails{ CompanyId = companies[0].Id, DateCreated = DateTime.Now, LastUpdated = DateTime.Now, Id = users[0].Id},
                new UserDetails{ CompanyId = companies[0].Id, DateCreated = DateTime.Now, LastUpdated = DateTime.Now, Id = users[1].Id}
            };

            userDetails.ForEach(u => context.UserDetails.Add(u));
            context.SaveChanges();

            var userroles = new List<UserRole>
            {
                new UserRole { Name ="Administrator", DateCreated= DateTime.Today, LastUpdated=DateTime.Today, Users = new List<User>()},
                new UserRole { Name ="Member", DateCreated = DateTime.Today, LastUpdated = DateTime.Today, Users = new List<User>()}
            };
            userroles.ForEach(r => context.UserRoles.Add(r));
            context.SaveChanges();

            userroles[0].Users.Add(users[0]);
            userroles[1].Users.Add(users[0]);
            userroles[1].Users.Add(users[1]);
            context.SaveChanges();

            var servicetypes = new List<ServiceType>
            {
                new ServiceType{ Name = "ServiceType1"},
                new ServiceType{ Name = "ServiceType2"},
                new ServiceType{ Name = "ServiceType3"},
                new ServiceType{ Name = "ServiceType4"},
                new ServiceType{ Name = "ServiceType5"}
            };
            servicetypes.ForEach(s => context.ServiceTypes.Add(s));
            context.SaveChanges();

            var chargecategories = new List<ChargeCategory>
            {
                new ChargeCategory{ Name = "ChargeCategory1", ServiceTypes= new List<ServiceType>()},
                new ChargeCategory{ Name = "ChargeCategory2", ServiceTypes= new List<ServiceType>()},
                new ChargeCategory{ Name = "ChargeCategory3", ServiceTypes= new List<ServiceType>()},
                new ChargeCategory{ Name = "ChargeCategory4", ServiceTypes= new List<ServiceType>()},
            };
            chargecategories.ForEach(c => context.ChargeCategories.Add(c));
            context.SaveChanges();

            chargecategories[0].ServiceTypes.Add(servicetypes[0]);
            chargecategories[0].ServiceTypes.Add(servicetypes[1]);
            chargecategories[0].ServiceTypes.Add(servicetypes[2]);
            chargecategories[1].ServiceTypes.Add(servicetypes[3]);
            chargecategories[2].ServiceTypes.Add(servicetypes[4]);
            chargecategories[3].ServiceTypes.Add(servicetypes[4]);
            context.SaveChanges();

            var stores = new List<Store>
            {
                new Store { Name = "Store1", CompanyId = companies[0].Id, Phone="1234567890", Address = address, ChargeCategoryId = chargecategories[0].Id},
                new Store { Name = "Store2", CompanyId = companies[0].Id, Phone="1234567890", Address = address, ChargeCategoryId = chargecategories[1].Id},
                new Store { Name = "Store3", CompanyId = companies[0].Id, Phone="1234567890", Address = address, ChargeCategoryId = chargecategories[2].Id},
                new Store { Name = "Store4", CompanyId = companies[0].Id, Phone="1234567890", Address = address, ChargeCategoryId = chargecategories[3].Id},
            };
            stores.ForEach(s => context.Stores.Add(s));
            context.SaveChanges();

            var items = new List<Item>();
            var clients = new List<Client>();
            for (int i = 0; i < 30; i++)
            {
                items.Add(new Item { Name = "Item" + (i + 1), ServiceTypeId = servicetypes[i % 5].Id, Price = ((i % 5) + 1.50) });
                var personName = new PersonName { FirstName = "Firstname" + (i + 1), LastName = "Lastname" + (i + 1), MiddleName = "Middlename" + (i + 1) };
                clients.Add(new Client { PersonName = personName, Phone = "0123456789", StoreId = stores[i % 4].Id, Created = DateTime.Now, LastAccessed = DateTime.Now, LastEdited = DateTime.Now });
            }
            items.ForEach(i => context.Items.Add(i));
            context.SaveChanges();
            clients.ForEach(c => context.Clients.Add(c));
            context.SaveChanges();

            var tickets = new List<Ticket>();
            for (int i = 0; i < 30; i++)
            {
                for (int y = 0; y < 20; y++)
                {
                    tickets.Add(new Ticket { ClientId = clients[i].Id, Name = (((i + 1) * 10000) + y).ToString(), TicketBalance = 0, TicketItems = new List<TicketItem>() });
                }
            }
            tickets.ForEach(t => context.Tickets.Add(t));
            context.SaveChanges();

            var ticketItems = new List<TicketItem>();
            for (int i = 0; i < 20; i++)
            {
                for (int y = 0; y < 25; y++)
                {
                    ticketItems.Add(new TicketItem { ItemId = items[y].Id, Name = items[y].Name, Price = items[y].Price, TicketId = tickets[0].Id });
                }
            }
            ticketItems.ForEach(t => context.TicketItems.Add(t));
            context.SaveChanges();

        }
        private string HashPassword(string password)
        {
            int workfactor = 12;
            return BCrypt.Net.BCrypt.HashPassword(password, workfactor);
        }
    }
}
